﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace EnergyDB1
{
    public class CollapseDataGridViewCell : DataGridViewTextBoxCell
    {
        #region 变量

        private int groupLevel;                                      //Cell所属层级，最上层为1，逐级递增
        private bool isParent;                                       //Cell是否有子节点
        private bool isCollapsed;                                    //Cell的状态是折叠还是展开
        private Rectangle plusRect;                                  //父节点折叠图标的区域
        private List<CollapseDataGridViewCell> childCells;           //Cell的子节点集合

        #endregion

        #region 属性

        public Rectangle PlusRect
        {
            get { return this.plusRect; }
            set { this.plusRect = value; }
        }

        public int GroupLevel
        {
            get { return this.groupLevel; }
            set { this.groupLevel = value; }
        }

        public bool IsParent
        {
            get { return this.isParent; }
            set { this.isParent = value; }
        }

        public bool IsCollapsed
        {
            get { return this.isCollapsed; }
            set { this.isCollapsed = value; }
        }

        public CollapseDataGridViewCell[] ChildCells
        {
            get
            {
                if (childCells == null)
                    return null;
                else
                    return childCells.ToArray();
            }
        }

        #endregion

        #region 构造函数

        public CollapseDataGridViewCell()
        {
            groupLevel = 1;
            isParent = false;
            isCollapsed = true;
        }

        #endregion

        #region 事件

        //重写DataGridViewCell的OnMouseDown事件，当鼠标单击父节点的折叠图标时，完成子节点的展开和收缩
        protected override void OnMouseDown(DataGridViewCellMouseEventArgs e)
        {
            Rectangle rect = DataGridView.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);
            Point pt = new Point(rect.Left + e.Location.X, rect.Top + e.Location.Y);
            if (plusRect.Contains(pt) && isParent)
            {
                if (isCollapsed)
                {
                    Expand();
                }
                else
                {
                    Collapse();
                }
            }
            base.OnMouseClick(e);
        }

        #endregion

        #region 方法

        //添加子节点
        public int AddChildCell(CollapseDataGridViewCell cell)
        {
            if (childCells == null)
            {
                childCells = new List<CollapseDataGridViewCell>();
            }
            childCells.Add(cell);
            cell.groupLevel = groupLevel + 1;

            return childCells.Count;
        }

        //展开子节点
        public void Expand()
        {
            isCollapsed = false;
            ShowChild(true);
            base.DataGridView.InvalidateCell(this);
        }

        //收起子节点
        public void Collapse()
        {
            isCollapsed = true;
            ShowChild(false);
            base.DataGridView.InvalidateCell(this);
        }

        //绘制子节点，展开时显示，收起时隐藏
        public void ShowChild(bool show)
        {
            if (childCells == null)
            {
                return;
            }
            foreach (CollapseDataGridViewCell cel in childCells)
            {
                if (cel.RowIndex == -1)
                {
                    continue;
                }
                DataGridView.Rows[cel.RowIndex].Visible = show;
                if (!cel.isCollapsed)
                {
                    cel.ShowChild(show);
                }
                //一旦父节点收起，则将其所有子节点的isCollapse设置为true
                if (!show)
                {
                    cel.isCollapsed = true;
                }
            }
        }

        #endregion
    }
}